package org.opicasso.cheese.ui.pages;

import org.apache.wicket.markup.html.WebMarkupContainer;
import org.apache.wicket.markup.html.form.Button;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.markup.html.link.PageLink;
import org.apache.wicket.markup.html.panel.FeedbackPanel;
import org.apache.wicket.model.CompoundPropertyModel;
import org.apache.wicket.model.PropertyModel;
import org.apache.wicket.validation.validator.PatternValidator;
import org.apache.wicket.validation.validator.StringValidator;
import org.opicasso.cheese.domain.Address;
import org.opicasso.cheese.domain.Cart;
import org.opicasso.cheese.ui.components.ShoppingCartPanel;

public class CheckoutPage extends AbstractCheesePage {

	private static final long serialVersionUID = -4647580960867333259L;

	public CheckoutPage() {
		// form feedback
		add(new FeedbackPanel("feedback"));

		// form
		final Form orderForm = new Form("orderForm");
		add(orderForm);

		// form fields
		final Address address = getCart().getBillingAddress();
		orderForm.add(new TextField("name", new PropertyModel(address, "name"))
				.setRequired(true).add(StringValidator.lengthBetween(2, 10)));
		orderForm.add(new TextField("street", new PropertyModel(address,
				"street")).setRequired(true));
		orderForm.add(new TextField("postalCode", new PropertyModel(address,
				"postalCode")).setRequired(true).add(
				new PatternValidator(
						"[a-zA-Z][0-9][a-zA-Z] *[0-9][a-zA-Z][0-9]")));
		orderForm.add(new TextField("city", new PropertyModel(address, "city"))
				.setRequired(true));

		// cancel
		orderForm.add(new Link("cancel") {
			private static final long serialVersionUID = -2853986624934246265L;

			@Override
			public void onClick() {
				setResponsePage(HomePage.class);
			}
		});

		// order
		orderForm.add(new Button("order") {
			private static final long serialVersionUID = -2853986624934246265L;

			@Override
			public void onSubmit() {
				final Cart cart = getCart();
				cart.getCheeses().clear();
				setResponsePage(HomePage.class);
			}
		});

		// cart
		add(new ShoppingCartPanel("cart", getCart()));

		// customer
		final CompoundPropertyModel customerModel = new CompoundPropertyModel(getCustomer());
		final Form customerForm = new Form("customerForm", customerModel);
		add(customerForm);
		customerForm.add(new TextField("firstName"));
		customerForm.add(new TextField("lastName"));
		final WebMarkupContainer addressComponent = new WebMarkupContainer(
				"address");
		customerForm.add(addressComponent);
		addressComponent.add(new TextField("street", customerModel
				.bind("address.street")));
		customerForm.add(new Link("cancel") {
			private static final long serialVersionUID = 3264104140926223530L;

			@Override
			public void onClick() {
				setResponsePage(HomePage.class);
			}
		});
		customerForm.add(new Button("submit") {
			private static final long serialVersionUID = 965837699742047948L;

			@Override
			public void onSubmit() {
				setResponsePage(HomePage.class);
			}
		});
		
	}
}
